﻿using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;

namespace Songhay.OpenXml.Transformations
{
    using Songhay.OpenXml.Models;

    /// <summary>
    /// Transforms the Open XML WordML <c>document</c> element.
    /// </summary>
    [Export(typeof(IWordProcessingDocumentTransformation))]
    public class DocumentTransformation : IWordProcessingDocumentTransformation
    {
        #region IWordProcessingDocumentTransform Members

        /// <summary>
        /// Gets or sets the execution ordinal.
        /// </summary>
        /// <value>The execution ordinal.</value>
        public byte ExecutionOrdinal { get; set; }

        /// <summary>
        /// Determines whether this instance can transform the specified word doc.
        /// </summary>
        /// <param name="wordDoc">The Word document.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="node">The node.</param>
        /// <returns>
        /// 	<c>true</c> if this instance can transform the specified word doc; otherwise, <c>false</c>.
        /// </returns>
        public bool CanTransform(WordprocessingDocument wordDoc,
            SonghayHtmlConverterSettings settings, XNode node)
        {
            return this.HasElementName(node, SonghayW.document);
        }

        /// <summary>
        /// Transforms the specified Word document.
        /// </summary>
        /// <param name="wordDoc">The Word document.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="node">The node.</param>
        /// <param name="imageHandler">The image handler.</param>
        public object Transform(WordprocessingDocument wordDoc,
            SonghayHtmlConverterSettings settings, XNode node,
            Func<ImageInfo, XElement> imageHandler)
        {
            var contentType = settings.UseEntityMap ?
                "text/html; charset=windows-1252"
                : "text/html; charset=utf-8";
            var generator = "PowerTools for Open XML";

            var titleElement = (settings.PageTitle != null) ?
                        new XElement(SonghayXhtml.title, settings.PageTitle)
                        : null; //TODO: get Word Document Title

            var cssObjects = (settings.Css != null) ?
                new XElement(SonghayXhtml.style,
                        new XComment(Environment.NewLine +
                        settings.Css + Environment.NewLine))
                        : null;

            XElement element = node as XElement;
            return new XElement(SonghayXhtml.html,
                new XElement(SonghayXhtml.head,
                    new XElement(SonghayXhtml.meta,
                        new XAttribute(SonghayXhtml.http_equiv, "Content-Type"),
                        new XAttribute(SonghayXhtml.content, contentType)),
                    new XElement(SonghayXhtml.meta,
                        new XAttribute(SonghayXhtml.name, "Generator"),
                        new XAttribute(SonghayXhtml.content, generator)),
                    titleElement, cssObjects),
                element.Elements()
                    .Select(e => OpenXmlUtility.TransformPackageNode(wordDoc, settings, e, imageHandler)));
        }

        #endregion
    }
}
